//
//  MSLNewsMainView.m
//  MSLNews
//
//  Created by lirenqiang on 16/6/12.
//  Copyright © 2016年 lirenqiang. All rights reserved.
//

#import "MSLNewsMainView.h"
#import "MSLChannelModel.h"
#import "MSLNewsMainViewCell.h"

static NSString * const MSLNewsMainViewCellId = @"MSLNewsMainViewCellId";

@interface MSLNewsMainView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

@implementation MSLNewsMainView {
    NSArray <MSLChannelModel *> *_channelModel;
}

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(nonnull UICollectionViewLayout *)layout
{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        
        self.delegate = self;
        self.dataSource = self;
        self.pagingEnabled = YES;
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        
        [self registerClass:[MSLNewsMainViewCell class] forCellWithReuseIdentifier:MSLNewsMainViewCellId];
        
        //注册通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getChannelViewModel:) name:MSLChannelViewModelNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jumpToSpecifiedCell:) name:MSLJumpNotification object:nil];
        
    }
    return self;
}

- (void)jumpToSpecifiedCell:(NSNotification *)nf {
 
    NSIndexPath * indexPath = nf.object;
    
    [self scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    
}

- (void)getChannelViewModel:(NSNotification *)nf {
    
    _channelModel = nf.object;
    
    [self reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
    return _channelModel.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MSLNewsMainViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:MSLNewsMainViewCellId forIndexPath:indexPath];
    
    cell.channelModel = _channelModel[indexPath.item];
    
    
    return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    
    
}



-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.bounds.size;
}


@end
